﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Rules;

namespace Validation
{
    public class NumbericMaxValidation : ValidationBase
    {
        public double MaxValue { get; set; }
        public bool IsEqual { get; set; }
        public NumbericMaxValidation(Csla.Core.IPropertyInfo PrimaryProperty, string ValidationMessage, object MaxValue, bool IsEqual)
            : base(PrimaryProperty, ValidationMessage)
        {
            this.MaxValue = double.Parse(MaxValue.ToString());
            this.IsEqual = IsEqual;
            InputProperties = new List<Csla.Core.IPropertyInfo> { PrimaryProperty };
        }

        protected override void Execute(RuleContext context)
        {
            object primaryValue = context.InputPropertyValues[PrimaryProperty];
            if (primaryValue != null)
            {
                bool expressionCheck = IsEqual ? double.Parse(primaryValue.ToString()) > MaxValue : double.Parse(primaryValue.ToString()) >= MaxValue;
                if (expressionCheck)
                {
                    context.AddErrorResult(this.ValidationMessage);
                    return;
                }
            }            
        }
    }
}
